package com.example.vbloodbanksalesforce;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.example.vbloodbanksalesforce.bean.BloodDetail;
import com.example.vbloodbanksalesforce.service.HttpUrlConnectionClass;
import com.example.vbloodbanksalesforce.service.Session;

public class DonorDetails extends BaseActivity{
	ArrayList<BloodDetail> arealist;
	HttpUrlConnectionClass httpUrl;
	private ProgressDialog mDialog;
	Spinner spArea,spBloodgroup;
	LinearLayout llLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blooddetails);
		llLayout = (LinearLayout)  findViewById(R.id.llGlobal);
		llLayout.setVisibility(View.GONE);
		
		spArea =(Spinner) findViewById(R.id.spArea);
		spBloodgroup =(Spinner) findViewById(R.id.spBdgroup);
		httpUrl = new HttpUrlConnectionClass(null);
		httpUrl.attach(this);

		
		
		
		
		String instanceUrl = Session.sInstanceUrl+"/services/data/v26.0/query/?q=Select+DonarArea__c+from+DonorRegistration__c";
		httpUrl.execute(instanceUrl, "retrive");
		
		OnCancelListener onCancelListener = new OnCancelListener() {
			public void onCancel(DialogInterface arg0) {      
				DialogCancelled();
				finish();
			}
		};                   

		mDialog = new ProgressDialog(this);
		mDialog.setMessage("Loading...");
		mDialog.setCanceledOnTouchOutside(false);                           
		mDialog.setIndeterminate(true);                           
		mDialog.setCancelable(true);
		mDialog.setOnCancelListener(onCancelListener);
		mDialog.show();          
	}
	public void DialogCancelled() {                       

		httpUrl.cancel(true);                                 
		
	}
	
	@Override
	public void apiReturn(String response) {
		// TODO Auto-generated method stub
		
		Log.i("NCS","Area  Response is   :"+response);  
		
		if(response!=null){
			mDialog.dismiss();
		}
		llLayout.setVisibility(View.VISIBLE);       
		arealist = new ArrayList<BloodDetail>();
		arealist.clear();
		try {
			JSONObject jobj = new JSONObject(response);
			JSONArray jarray = jobj.getJSONArray("records");
			for (int i = 0; i < jarray.length(); i++) {
				BloodDetail bean = new BloodDetail();
				JSONObject jobj2 = jarray.getJSONObject(i);
				if (!jarray.isNull(i)) {
					if (!jobj2.isNull("DonarArea__c")) {
						String area = jobj2.getString("DonarArea__c");
						bean.setArea(area);
						Log.i("NCS","Area is   :"+area);
					}
					arealist.add(bean);
				}
				
			}
			
			refresh();
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void refresh(){
		/*AreaAdapter adapter = new AreaAdapter(this,arealist);
		spArea.setAdapter(adapter);*/
		
		List bloodBank = getBloodBank();

		ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),R.layout.display, bloodBank);               

		spArea.setAdapter(adapter);
		
                 
		
	}
	private List getBloodBank() {

		List list = new ArrayList();

		for (BloodDetail c : arealist)    

		{

		list.add(c.getArea());

		}

		return list;
		}
	
	
	public void getBloodDonarDetails(View v){
		
		
		
		String area = spArea.getSelectedItem().toString();
		
		String bloodgroup = spBloodgroup.getSelectedItem().toString();
		
		Intent i = new Intent(getApplicationContext(),DonorDetailsList.class);
		i.putExtra("K1", area);
		i.putExtra("K2", bloodgroup);
		startActivity(i);
		
	}
	

}
